package br.unicentro.lynx.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.unicentro.lynx.model.factory.ConnectionFactory;
import br.unicentro.lynx.model.pojo.Author;

public class AuthorDao {
	private String sql;
	private Connection connection;
	private PreparedStatement statement;
	private ResultSet result;

	public void insert(Author author) {
		open();
		this.sql = "INSERT INTO Authors (name_author, country_author, description_author) VALUES (?, ?, ?)";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setString(1, author.getNameAuthor());
			this.statement.setString(2, author.getCountryAuthor());
			this.statement.setString(3, author.getDescriptionAuthor());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void update(Author author) {
		open();
		this.sql = "UPDATE Authors SET name_author = ?, country_author = ?, description_author = ? WHERE id_author = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setString(1, author.getNameAuthor());
			this.statement.setString(2, author.getCountryAuthor());
			this.statement.setString(3, author.getDescriptionAuthor());
			this.statement.setInt(4, author.getIdAuthor());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void delete(Author author) {
		open();
		this.sql = "DELETE FROM Authors WHERE id_author = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, author.getIdAuthor());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public Author search(Integer id_author) {
		open();
		this.sql = "SELECT * FROM Authors WHERE id_author = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, id_author);
			this.result = this.statement.executeQuery();
			if (this.result.next()) {
				return new Author(result.getInt("id_author"),
						result.getString("name_author"),
						result.getString("country_author"),
						result.getString("description_author"));
			} else {
				return null;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public ArrayList<Author> list() {
		open();
		this.sql = "SELECT * FROM Authors";
		ArrayList<Author> authors = new ArrayList<Author>();
		try {
			this.statement = connection.prepareStatement(sql);
			this.result = this.statement.executeQuery();
			while (this.result.next()) {
				authors.add(new Author(result.getInt("id_author"), result
						.getString("name_author"), result
						.getString("country_author"), result
						.getString("description_author")));
			}
			return authors;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				this.result.close();
				close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}

	private void open() {
		this.connection = ConnectionFactory.getConnection(
				ConnectionFactory.URL, ConnectionFactory.USER,
				ConnectionFactory.PASSWORD);
	}

	private void close() {
		try {
			this.statement.close();
			this.connection.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
